package com.atguigu.mybatiesplus.controller;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author lihao
 * @className Test
 * @date 2023/10/20
 * @description
 */
public class Test {

    //接口
    static interface Subject{
        void sayHi();

        void sayHello();
    }

    //默认实现类
    static class SubjectImpl implements Subject{

        @Override
        public void sayHi() {
            System.out.println("hi");
        }

        @Override
        public void sayHello() {
            System.out.println("hello");
        }
    }

    // jdk动态代理
    static class ProxyInvocationHandler implements InvocationHandler{

        private Subject target;

        public ProxyInvocationHandler(Subject target){
            this.target = target;
        }

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            System.out.println("say:");
            return method.invoke(target,args);
        }
    }

    public static void main(String[] args) {
        Subject subject = new SubjectImpl();
        Subject subjectProxy = (Subject) Proxy.newProxyInstance(subject.getClass().getClassLoader(), subject.getClass().getInterfaces(), new ProxyInvocationHandler(subject));
        subjectProxy.sayHi();
        subjectProxy.sayHello();

    }
}
